package com.franklin.ideaplugin.api.json.fastjson2;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.writer.ObjectWriter;
import com.franklin.ideaplugin.api.beans.EnumClass;

import java.lang.reflect.Type;
import java.util.List;

/**
 * @author Ye Junhui
 * @since 2023/6/2
 */
public class EnumClassSerializer implements ObjectWriter<String> {
    @Override
    public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
        if (object == null) {
            jsonWriter.writeNull();
            return;
        }

        if (object instanceof EnumClass) {
            EnumClass enumClass = (EnumClass) object;
            List<String> enumList = enumClass.getEnumList();
            if (CollectionUtil.isNotEmpty(enumList)){
                String enumVal = enumList.get(0);
                jsonWriter.writeString(enumVal);
                return;
            }
        }
        jsonWriter.writeNull();
    }
}
